var app = angular.module('myApp', []);
app.config(function($interpolateProvider) {
    $interpolateProvider.startSymbol('[[');
    $interpolateProvider.endSymbol(']]');
});
app.controller('mdController', function($scope,$http) {

    var editor,writeId=null;
    $(function () {
        editor = editormd('write',{
            path:'/static/lib/editor.md/lib/',   //加载editorMd所需资源路径
            width:'100%',
            height:'99.91%',
            watch:false,
            emoji:true,
            flowChart:true,
            sequenceDiagram : true,
            imageUpload:true,
            imageFormats : ["jpg", "jpeg", "gif", "png", "bmp", "webp"],
            imageUploadURL : "./php/upload.php?test=dfdf",

//                    /*
//                     上传的后台只需要返回一个 JSON 数据，结构如下：
//                     {
//                        success : 0 | 1,           // 0 表示上传失败，1 表示上传成功
//                        message : "提示的信息，上传成功或上传失败及错误信息等。",
//                        url     : "图片地址"        // 上传成功时才返回
//                     }
//                     */
            toolbarIconsClass : {
                testIcon : "fa-floppy-o"  // 指定一个FontAawsome的图标类
            },
            toolbarHandlers:{
                testIcon:function (cm,icon,cursor,selection) {
                    var title=$('input[name=title]').val();
                    if(writeId!=undefined&&writeId!=null){
                        $.ajax({
                            type:'post',
                            url:'/api/modifyWrite',
                            dataType:'json',
                            data:{content:this.getMarkdown(),title:title,id:writeId},
                            success:function () {
                                alert('success')
                                $scope.getTypeList();
                            },
                            error:function () {
                                alert('error')
                            }
                        })
                    }else {
                        $.ajax({
                            type:'post',
                            url:'/api/addWrite',
                            dataType:'json',
                            data:{content:this.getMarkdown(),title:title,typeId:$scope.typeId},
                            success:function () {
                                alert('success')
                            },
                            error:function () {
                                alert('error')
                            }
                        })
                    }

                }
            },
            lang:{
                toolbar:{
                    testIcon:'保存',
                }
            },
            toolbarIcons:function () {
                return [
                    "undo", "redo", "|",
                    "bold", "del", "italic", "quote", "uppercase", "lowercase", "|",
                    "h1", "h2", "h3", "h4", "h5", "h6", "|",
                    "list-ul", "list-ol", "hr", "|",
                    "watch", "preview", "fullscreen", "|",
                    "help",'testIcon'
                ]
            },
            onfullscreen : function() {
                editor = editormd('write',{
                    toolbarIcons:function () {
                        return editormd.toolbarModes.full
                    }
                })
            },
            onfullscreenExit:function () {
                editor = editormd('write',{
                    toolbarIcons:function () {
                        return [
                            "undo", "redo", "|",
                            "bold", "del", "italic", "quote", "uppercase", "lowercase", "|",
                            "h1", "h2", "h3", "h4", "h5", "h6", "|",
                            "list-ul", "list-ol", "hr", "|",
                            "watch", "preview", "fullscreen", "|",
                            "help",'testIcon'
                        ]
                    }
                })
            },


        })
    });




    $scope.noteForm=true;
    $scope.typeId = null;

    //获取笔记分类
    $scope.getTypeList=function () {
        $http({
            method: 'GET',
            url: '/api/typeList'
        }).then(function (r) {
            $scope.typeList = r.data;
            $scope.typeId = r.data[0].id
            $scope.getTypeNode(r.data[0].id);
            $scope.mdCount = 0;
        }, function (response) {
            // 请求失败执行代码
        });
    }
    $scope.getTypeList();

    //新增分类
    $scope.newNotebook = function (noteName) {
        if(noteName==''|| noteName==undefined){
            alert('请输入分类名称')
        }else {
            $http({
                method:'POST',
                url:'/api/addType',
                data:{name:noteName}
            }).then(function (r) {
                $scope.getTypeList();
                $scope.addNote=false
                $scope.noteName='';
            })
        }
    }
    
    $scope.count=0;
    $scope.mdCount=null;
    $scope.settingCount=false;
    $scope.settingShow = function (index,id) {
        $scope.count=index;
        $scope.settingCount=false
        $scope.getTypeNode(id);
        $scope.mdCount = 0;
        $scope.typeId = id;
    };

    $scope.modifyTypeName = function (item) {
        let r =prompt('修改名字',item.name)
        if(r){
            $http({
                method:'POST',
                url:'/api/modifyTypeName',
                data:{id:item.id,name:r}
            }).then(function (r) {
                $scope.getTypeList();
            })
        }else{}
    }
    $scope.deleteType = function (item) {
        let r = confirm('确认删除'+item.name+'?')
        if (r==true){
            $http({
                method:'POST',
                url:'/api/deleteType',
                data:{id:item.id}
            }).then(function (r) {
                $scope.getTypeList();
            })
        }else {}
    };

    $scope.getTypeNode = function (id) {
        $http({
            method:'GET',
            url:'/api/getTypeNote/'+id,
        }).then(function (r) {
            $scope.typeNode = r.data;
            if(r.data.length!==0){
                $scope.getNodeInfo(r.data[0].id);
                $scope.noteForm = true
                $('.write_main').css({paddingTop:'20px'})
            }else {
                writeId=null;
                $scope.noteForm = false
                $('.write_main').css({padding:'0px'})
            }

        })
    }


    $scope.toogleNote = function (index,id,$event) {
        $scope.mdCount = index;
        $scope.getNodeInfo(id);
        $scope.writeCount=false;
    };
    $scope.writeCount=false;
    $scope.writeSetting = function ($event) {
        $event.stopPropagation();
        $scope.writeCount=!$scope.writeCount;
    }

    //获取笔记内容
    $scope.getNodeInfo = function (id) {
        $http({
            method:'GET',
            url:'/api/information/'+id
        }).then(function (r) {
            $scope.noteBook = r.data[0]
            writeId = r.data[0].id;
            editor = editormd('write',{
                markdown:r.data[0].content
            });
        })
    };

    $scope.creatMD = function () {
        $scope.mdCount = 'creat';
        $scope.noteBook={};
        $scope.noteBook.title = '新建文本';
        writeId=null;
        $scope.noteForm = true
        $('.write_main').css({paddingTop:'20px'})
        editor = editormd('write',{
            markdown:null
        });
    };

    $scope.deleteWrite = function (item) {
        let r = confirm('确认删除'+item.title+'?')
        if (r==true){
            $http({
                method:'POST',
                url:'/api/deleteWrite',
                data:{id:item.id}
            }).then(function (r) {
                $scope.getTypeNode($scope.typeId);
            })
        }else {}
    }
    $scope.imageUpload = function () {
        $http({
            method:'POST',
            url:'/api/imageUpload',
            data:{file:$('#fiel').val()}
        }).then(function (r) {
            $scope.getTypeList();
        })
    }

});